<template>
  <view>
      {{ getTreeIdxInParent(rowIdx) + 1 }}
  </view>
</template>
<script>
export default {
  inheritAttrs: false,
};
</script>
<script setup>
import { computed } from "vue";


const props = defineProps({
  metaItem: {
    type: Object,
    required: true,
  },
  commonAttr: {
    type: Object,
    default: () => ({}),
  },
  vmTemplateConfig:{
    type:Object
  },
  //表格专用 开始
  rowIdx: {
    type: [Number, Object],
    default: () => null,
  },
  colIdx: {
    type: [Number, Object],
    default: () => null,
  },
  rowData: {
    type: Object,
    default: () => null,
  },
  tableBodyData: {
    type: Array,
  },
  flattenTableBodyData: {
    type: Array,
  },
  //表格专用 结束
  //表单专用 开始
  formData: {
    type: Object,
    default: () => null,
  },
  //表单专用 结束
});
computed(() => {
  metaItem.generateSerialNum;
});
/**
 * 树形表格的序号函数
 */
const getTreeIdxInParent = (rowIdx) => {
  const idKeyAlias = props.commonAttr["row-key"];
  const childrenKeyAlias =
    props.commonAttr["tree-props"]?.children ?? "children";
  const curRowId = props.flattenTableBodyData?.[rowIdx]?.[idKeyAlias];

  // 没有父级的先匹配
  const matchFirstLevelIdx = props.tableBodyData.findIndex(
    (el) => el[idKeyAlias] === curRowId
  );
  if (matchFirstLevelIdx !== -1) {
    console.log(matchFirstLevelIdx,'matchFirstLevelIdx=======')
    return matchFirstLevelIdx;
  } else {
    // 有父级的找到父级
    const matchParentEl = props.flattenTableBodyData.find(
      (el) =>
        Array.isArray(el[childrenKeyAlias]) &&
        el[childrenKeyAlias].some((el2) => el2[idKeyAlias] === curRowId)
    );

    const matchChildIdx =
      matchParentEl?.[childrenKeyAlias].findIndex(
        (el) => el[idKeyAlias] === curRowId
      ) ?? 0;
    console.log(matchChildIdx,'matchChildIdx=======')
    return matchChildIdx;
  }
};
</script>
